package cn.com.aiouyi.controller.admin;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import cn.com.aiouyi.common.Message;
import cn.com.aiouyi.entity.PhotoPosition;
import cn.com.aiouyi.page.Pageable;
import cn.com.aiouyi.service.PhotoPositionService;

/**
 * Controller - 导航位
 * @author hanson
 *
 */
@Controller("adminPhotoPositionController")
@RequestMapping("/admin/photoPosition")
public class PhotoPositionController extends BaseController{

	@Resource(name = "photoPositionServiceImpl")
	private PhotoPositionService photoPositionService;
	
	@GetMapping("/list")
	public String list(Pageable pageable, ModelMap model){
		
		model.put("page", photoPositionService.findPage(pageable));
		return "/admin/photoPosition/list";
	}
	
	@GetMapping("/add")
	public String add(ModelMap model){
		
		model.put("codes", PhotoPosition.Code.values());
		return "/admin/photoPosition/add";
	}
	
	@PostMapping("/save")
	public String save(@Valid PhotoPosition photoPosition, RedirectAttributes redirectAttributes){
		
		photoPositionService.save(photoPosition);
		
		addFlashMessage(redirectAttributes, Message.SUCCESS);
		return "redirect:list";
	}
	
	@GetMapping("/edit")
	public String edit(Long id, ModelMap model){
		
		model.put("photoPosition", photoPositionService.find(id));
		model.put("codes", PhotoPosition.Code.values());
		return "/admin/photoPosition/edit";
	}
	
	@PostMapping("/update")
	public String update(Long id, @Valid PhotoPosition photoPosition, RedirectAttributes redirectAttributes){
		
		photoPositionService.update(photoPosition);
		
		addFlashMessage(redirectAttributes, Message.SUCCESS);
		return "redirect:list";
	}
	
	@PostMapping("/delete")
	@ResponseBody
	public Message delete(Long[] ids){
		
		photoPositionService.realDelete(ids);
		
		return Message.SUCCESS;
	}
}
